package com.taylover.test.character;

import com.taylover.test.exception.IndexIsNagetiveException;
import com.taylover.test.exception.IndexIsOutofRangeException;

/**
 * @author xyzZero3
 * @date 2025/4/19 19:57
 */
public class MyStringBuffer implements IStringBuffer {
    private String str = "";

    public MyStringBuffer() {
    }

    public MyStringBuffer(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return this.str;
    }

    @Override
    public void append(String str) {
        this.str = this.str.concat(str);
    }

    @Override
    public void append(char c) {
        this.str = this.str.concat(String.valueOf(c));
    }

    @Override
    public void insert(int pos, char b) throws IndexIsNagetiveException, IndexIsOutofRangeException {
        if (pos < 0) {
            throw new IndexIsNagetiveException();
        }
        if (pos  > this.str.length()) {
            throw new IndexIsOutofRangeException();
        }
        this.str = str.substring(0, pos).concat(String.valueOf(b)).concat(str.substring(pos));
    }

    @Override
    public void insert(int pos, String b) throws IndexIsNagetiveException, IndexIsOutofRangeException {
        if (pos < 0) {
            throw new IndexIsNagetiveException();
        }
        if (pos  > this.str.length()) {
            throw new IndexIsOutofRangeException();
        }
        if (null == b) {
            throw new NullPointerException("添加的字符串不能为空");
        }
        this.str = str.substring(0, pos).concat(b).concat(str.substring(pos));
    }

    @Override
    public void delete(int start) throws IndexIsNagetiveException, IndexIsOutofRangeException {
        if (start < 0) {
            throw new IndexIsNagetiveException();
        }
        if (start > this.str.length()) {
            throw new IndexIsOutofRangeException();
        }
        this.str = this.str.substring(0, start);
    }

    @Override
    public void delete(int start, int end) throws IndexIsNagetiveException, IndexIsOutofRangeException {
        if (start < 0) {
            throw new IndexIsNagetiveException();
        }
        if (start > this.str.length()) {
            throw new IndexIsOutofRangeException();
        }
        if (end < 0) {
            throw new IndexIsNagetiveException();
        }
        if (end > this.str.length()) {
            throw new IndexIsOutofRangeException();
        }
        if (start > end) {
            throw new IndexIsOutofRangeException();
        }
        if (start == 0) {
            this.str = this.str.substring(end + 1, this.str.length());
        } else {
            this.str = this.str.substring(0, start).concat(this.str.substring(end + 1, this.str.length()));
        }
    }

    @Override
    public void reverse() {
        char[] charArray = this.str.toCharArray();
        for (int i = 0; i < charArray.length / 2; i++) {
            char temp = charArray[i];
            charArray[i] = charArray[charArray.length - i - 1];
            charArray[charArray.length - i - 1] = temp;
        }
        this.str = new String(charArray);
    }

    @Override
    public int length() {
        return this.str.length();
    }
}
